package com.ruoyi.web.controller.report;

import com.ruoyi.blockPlan.domain.BlockPlanT;
import com.ruoyi.blockPlan.service.IBlockPlanTService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.data.MD5;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.unit.service.IHouseUnitTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 返还Controller
 * 
 * @author lijie
 * @date 2023-05-04
 */
@RestController
@RequestMapping("/report/blockPlan")
public class BlockPlanTController extends BaseController
{
    @Autowired
    private IBlockPlanTService blockPlanTService;

    @Autowired
    private IHouseUnitTService houseUnitTService;

    private int floorNum = 0;

    private int doorNum = 0;

    private int minfloorNum = 0;

    private int mindoorNum = 0;
    /**
     * 查询返还列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BlockPlanT blockPlanT)
    {

        startPage();
        List<Map<Object,Object>> list02 = new ArrayList<Map<Object,Object>>();
        if(blockPlanT.getAreaName()!=null&&blockPlanT.getGroupName()!=null){
            List<BlockPlanT> utilList = blockPlanTService.selectHouseUnitTList(blockPlanT);
//        Map<Object,Object> map02 = new HashMap<Object,Object>();
            for(BlockPlanT blockPlanVO:utilList){
                Map<Object,Object> map01 = new HashMap<Object,Object>();
                List<Map<Object,Object>> list01= this.getNoDoorList(blockPlanVO);
                map01.put("danyuan",list01);
                map01.put("shunxu",blockPlanVO.getUnitName());
                list02.add(map01);
            }
        }
//        map02.put("louyu",list02);
        return getDataTable(list02);
    }

    public List<Map<Object,Object>> getNoDoorList(BlockPlanT blockPlanVO){
        floorNum = 0;
        doorNum = 0;
        minfloorNum = 0;
        mindoorNum = 0;
        List<BlockPlanT> doorList = new ArrayList<BlockPlanT>();
        BlockPlanT door;
        int result = 0;
        doorList = blockPlanTService.selectHouseDoorTList(blockPlanVO);
        this.getFloorAndDoor(doorList);
        List<Map<Object,Object>> list = new ArrayList<Map<Object,Object>>();
        for(int doorrV = mindoorNum ; doorrV <= doorNum ;doorrV++) {
            Map<Object,Object> map = new HashMap<Object,Object>();
            List<BlockPlanT> blockList = new ArrayList<BlockPlanT>();
            for(int floorV = 1 ; floorV <= floorNum ;floorV++) {
                BlockPlanT blockVO = new BlockPlanT();
                String doorId = "";
                doorId = String.valueOf(floorV) + String.format("%" + 2 + "d", doorrV).replace(" ", "0");
                door = new BlockPlanT();
                blockPlanVO.setDoorName(doorId);
                blockVO.setDoorName(doorId);
                door = blockPlanTService.getDoorDetailTemp(blockPlanVO);
                if(door == null) {
                    blockVO.setType("2");
                    blockList.add(blockVO);
                }else if((door.getBalance()/door.getBudgetMoney())>0.3) {
                    blockVO.setDoorId(door.getDoorId());
                    blockVO.setType("0");
                    blockList.add(blockVO);
                }else{
                    blockVO.setDoorId(door.getDoorId());
                    blockVO.setType("1");
                    blockList.add(blockVO);
                }
            }
            map.put("hu",blockList);
            list.add(map);
        }

        return list;
    }

    /**
     * 获取户管理详细信息
     */
    @GetMapping(value = "/{doorId}")
    public AjaxResult getInfo(@PathVariable("doorId") Long doorId) throws Exception {
        BlockPlanT houseDoorT = blockPlanTService.selectHouseDoorTByDoorId(doorId);
        if (houseDoorT.getPayMoney()==null){
            houseDoorT.setPayMoney(0.0);
        }
        houseDoorT.setPapersCode(MD5.decrypt(houseDoorT.getPapersCode()));
        houseDoorT.setUserName(MD5.decrypt(houseDoorT.getUserName()));
        houseDoorT.setTel(MD5.decrypt(houseDoorT.getTel()));
        return AjaxResult.success(houseDoorT);
    }

    public void getFloorAndDoor(List<BlockPlanT> doorList){
        mindoorNum = 100;
        for(int j=0;j<doorList.size();j++){
            int floor = Integer.valueOf(doorList.get(j).getDoorName().substring(0, doorList.get(j).getDoorName().length()-2));
            if(floor>floorNum){
                floorNum = floor;
            }
            if(floor<minfloorNum){
                minfloorNum = floor;
            }
            int door = Integer.valueOf(doorList.get(j).getDoorName().substring(doorList.get(j).getDoorName().length()-2,doorList.get(j).getDoorName().length()));
            if(door>doorNum){
                doorNum = door;
            }
            if(door<mindoorNum){
                mindoorNum = door;
            }
        }
    }

}
